package com.chushouya.manager.dao.repository;


import com.general.framework.core.lang.Strings;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.ActivityLogEntity;
import com.chushouya.manager.dao.mapper.ActivityLogEntityMapper;
import com.chushouya.manager.dto.admin.activity.ActivityLogQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

@Repository
public class ActivityLogRepository extends BaseRepository<ActivityLogEntityMapper, ActivityLogEntity, Long> {

    public ActivityLogEntity selectByActivityIdAndUserId(Long activityId, Long userId) {
        Example example = new Example(ActivityLogEntity.class);
        example.createCriteria().andEqualTo("activityId", activityId).andEqualTo("userId", userId);
        return selectFirstOneByExample(example);
    }

    public ActivityLogEntity selectOne(ActivityLogQuery query) {
        Example example = buildExample(query);
        return selectFirstOneByExample(example);
    }

    private Example buildExample(ActivityLogQuery query) {
        Example example = new Example(ActivityLogEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getActivityId())) {
            criteria.andEqualTo("activityId", query.getActivityId());
        }
        if(Objects.nonNull(query.getUserId())){
            criteria.andEqualTo("userId", query.getUserId());
        }
        if(Strings.isNotBlank(query.getAppId())){
            criteria.andEqualTo("appId", query.getAppId());
        }
        return example;
    }
}
